package uplift.project;

import java.util.ArrayList;

import uplift.project.model.VolunteerEvent;
import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class SearchResultsActivity extends ListActivity{
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.suggestions_list);
		
        Intent startIntent = this.getIntent();
        Bundle extras = startIntent.getExtras();
        String category = extras.getString("category");
        String location = extras.getString("location");
        String date = extras.getString("date");
        String startTime = extras.getString("starts");
        String endTime = extras.getString("ends");
		

		ApplicationData appData = (ApplicationData)this.getApplicationContext();
		ArrayList<VolunteerEvent> vList = appData.eventsList.searchEvent(category, location, date, startTime, endTime);
		//ArrayList<VolunteerEvent> vList = appData.eventsList.getUnassociatedEvents();
		
		BrowseAdapter adapter = new BrowseAdapter(this, android.R.layout.simple_list_item_1, vList);

		this.getListView().setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View
					v, int position, long id) {
				Intent i = new Intent(v.getContext(), JoinEventActivity.class);
				VolunteerEvent item = (VolunteerEvent)a.getItemAtPosition(position);
				i.putExtra("name", item.getName());
				i.putExtra("category", item.getCategory());
				i.putExtra("location", item.getLocation());
				i.putExtra("date", item.getDate());
				i.putExtra("starts", item.getStartTime());
				i.putExtra("ends", item.getEndTime());
				i.putExtra("description", item.getDescription());
				startActivity(i);
			}
		});

		setListAdapter(adapter);

		
		// set the action bar to have up-navigation enabled
	    ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
	}


	// handles action item button presses
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			// move up the hierarchy
			finish();
		} else if(item.getItemId() == R.id.menu_home) {
			// go to Home screen
			
			Intent i = new Intent(getListView().getContext(), UpliftActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		} else if(item.getItemId() == R.id.menu_my_events) {
			// go to My Events screen
			
			Intent i = new Intent(getListView().getContext(), MyEventsTabActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}
		return true;
	}
	
	// set up Action Bar
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.action_bar, menu);
	    return true;
	}
}